var jsonSuggestionData = {
    search_value: "",   
    "column-sort": "",
    "column-sort-type": "", 
}
var columnSort = [];
$(document).ready(function(){
	// Suggestion Box
	$("body").on("click",".suggestionBox",function(){
		Suggestion.modal_suggestion();
	});
	var pathname = window.location.pathname;
	if (pathname=="/suggestions") {
	    Suggestion.loadSuggestion();
	}else if(pathname=="/user_view/suggestions"){
		//Suggestion
	    $("body").on("keyup","#txtSearchSuggestionDatatable",function(e){
	        $("#loadSuggestion").dataTable().fnDestroy();
	        jsonSuggestionData['search_value'] = $(this).val();
	        Suggestion.defaultData();
	    })

		Suggestion.defaultData();
	}
	$("body").on("click","#loadSuggestion th",function(){
        var cursor = $(this).css("cursor");
        jsonSuggestionData['column-sort'] = $(this).attr("field_name");
        if($(this).attr("field_name")=="" || $(this).attr("field_name")==undefined){
            return;
        }
        var indexcolumnSort = columnSort.indexOf($(this).attr("field_name"));

        $(this).closest("#loadSuggestion").find(".sortable-image").html("");
        if(indexcolumnSort==-1){
            columnSort.push($(this).attr("field_name"))
            $(this).find(".sortable-image").html("<i class='fa fa-sort-asc'></i>")
            jsonSuggestionData['column-sort-type'] = "ASC";
        }else{
            columnSort.splice(indexcolumnSort,1);
            $(this).find(".sortable-image").html("<i class='fa fa-sort-desc'></i>")
            jsonSuggestionData['column-sort-type'] = "DESC";
        }
        
        $("#loadSuggestion").dataTable().fnDestroy();
        Suggestion.defaultData();
    })
})
Suggestion = {
	modal_suggestion : function(){
		var ret = "";
		ret += '<h3 class="fl-margin-bottom"><i class="icon-comment"></i> Your Suggestion</h3>';
		ret += '<div class="hr"></div>';
		ret += '<div class="fields">';
			ret += '<div class="label_below2"> Title:<font color="red">*</font> </div>';
			ret += '<div class="input_position"  style="width: 100%;">';
				ret += '<input type="text" class="suggestion-title fl-suggestion-input" maxlength="100" name="suggestion-title" placeholder="Title">';
			ret += '</div>';
			ret += '<br/>';
			ret += '<div class="label_below2"> Message:<font color="red">*</font> </div>';
			ret += '<div class="input_position" style="width: 100%;">';
				ret += '<textarea class="form-textarea_save suggestion-message" style="height:80px;resize:none; margin-top:0px;" name="" id="" placeholder="Message" style="width:100%;"></textarea>';
			ret += '</div>';
		ret += '</div>';
			ret += '<div class="label_below2"></div>';
			ret += '<div class="input_position" style="margin-top:5px;text-align:right;">';
				ret += '<img src="/images/loader/load.gif" class="display " style="margin-right:5px;margin-top:2px;margin-bottom:5px;">';
				ret += '<input type="button" class="btn-blueBtn fl-margin-right"  id="suggest-submit" value="Save"> ';
				ret += '<input type="button" class="btn-basicBtn" id="popup_cancel" value="Cancel" style="margin-bottom:5px;">';
			ret += '</div>';
		
		var newDialog = new jDialog(ret, "","550", "250", "", function(){
			
		});
		newDialog.themeDialog("modal2");
		//bind function on click of suggestion
		this.addSuggestion();
	},
	addSuggestion : function(){
		$("#suggest-submit").click(function(){
		   // jConfirm('Are you sure you want to submit your suggestion?', '', '', '', '', function(ans){
			  //   if(ans==true){
			    	var title = $(".suggestion-title").val();
					var message = $(".suggestion-message").val();
					$.post("/ajax/suggestion",{action:"addSuggestion",
										title:title,message:message},function(data){
						if(data>0){
							showNotification({
	                          	message: "Suggestion Added",
	                          	type: "success",
	                          	autoClose: true,
	                        	duration: 3
	                        })
						}
                        $("#popup_cancel").click();
					})
			  //   }
		   // });
		
		})
	},
	loadSuggestion : function(){
		//code here
		var oTable = $('#loadSuggestion').dataTable( {
                     "bProcessing": true,
                     "bRetrieve": true,
                     "aaSorting" : [[2,"desc"]],
                     //"bPaginate": true,//"sEcho":3,//"iTotalDisplayRecords":10,//"iTotalRecords":"10",
                     //"iTotalDisplayRecords":"10",//"bServerSide": true,
                     "sAjaxSource": "/ajax/suggestion",
                     "sPaginationType": "full_numbers",
                     "sAjaxDataProp": "company_user",
                     "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
                     "aoColumns": [
                         { "mData": "title" },
                         { "mData": "message" },
                         { "mData": "datetime" },
		 				 { "mData": "status" },
                         // { "mData": "actions" }
                     ],
                     "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
                         $("#example_length > label > select").addClass("selectlength");
                     },
                     "aoColumnDefs": [ {
                         "bSortable": true, "aTargets": [ 0,1,2,3 ]
                         },{
                             "bSortable": false, "aTargets": [ "_all" ]
                         }/*, {
                             "bSearchable": true, "aTargets": [ 0 ]
                         }, {
                             "bSearchable": false, "aTargets": [ "_all" ]
                     }*/
                     ],
                     "fnServerData": function (sSource, aoData, fnCallback) {
                         $.ajax({
                             type: "POST",
                             cache: false,
                             dataType: 'json',
                             url: sSource,
                             data: {action:"loadSuggestion"},
                             success: function (json) {
                                 // Retrieve data
                                 //console.log(json)
                                 fnCallback(json);
                             }
                         });
                     }
                 });
                return oTable;
	},
	viewFullSuggestion : function(){
		$(".viewFullSuggestion").click(function(){

		})
	},
	removeSuggestion : function(){
		//code here
		$(".deleteFullSuggestion").click(function(){

		})
	},
	"defaultData" : function(){
        var oTable = $('#loadSuggestion').dataTable( {
            "sDom": 'Rlfrtip',
            // "sScrollY": "400px",
            "oLanguage": {
                "sProcessing": '<div class="spinner load_m"> '+
                       '<div class="bar1"></div> '+
                       '<div class="bar2"></div> '+
                       '<div class="bar3"></div> '+
                       '<div class="bar4"></div> '+
                       '<div class="bar5"></div> '+
                       '<div class="bar6"></div> '+
                      '<div class="bar7"></div> '+
                      '<div class="bar8"></div> '+
                      '<div class="bar9"></div> '+
                      '<div class="bar10"></div> '+
                 '</div>'
            },
            "oColReorder": false,
            "bLengthChange": false,
            "bFilter": false,
            "bProcessing": true,
            "bServerSide": true,
            "sPaginationType": "full_numbers",
            "bSort": false,
            "sAjaxSource": "/ajax/suggestion",
            "fnServerData": function(sSource, aoData, fnCallback) {
                aoData.push({"name": "search_value", "value": jsonSuggestionData['search_value']});
                aoData.push({"name": "action", "value": "loadSuggestionDatatable"});
                aoData.push({"name": "column-sort", "value": jsonSuggestionData['column-sort']});
                aoData.push({"name": "column-sort-type", "value": jsonSuggestionData['column-sort-type']});
                $.ajax({
                    "dataType": 'json',
                    "type": "POST",
                    "url": sSource,
                    "data": aoData,
                    "success": function(data) {
                        console.log(data)
                        // return;
                        fnCallback(data);
                    }
                });
            },
            "fnInitComplete": function(oSettings, json) {
                // 
            }
        });
        return oTable;
    }
}